package communication;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Communicator {

	// private Reader reader = new Reader();
	private BTCommandWrapper btCommandWrapper;

	public Communicator() {

		BTConnection connection = new BTConnection();
		
		btCommandWrapper = new BTCommandWrapper(connection);
		
		
		Thread bt_thread = new Thread(connection);
		bt_thread.start();
		try {
			bt_thread.join();
		} catch (InterruptedException e) {
			System.out.println("BTException");
		}

	}


	public void travel(int distance) {
		System.out.println("start travel");

		btCommandWrapper.SendCommandAndReceive(Command.TRAVEL, distance);

		
		System.out.println("stop travel");
	}

	

	public void terminate() {
		btCommandWrapper.SendCommandAndReceive(Command.TERMINATE, 0);

		
	}

	// positief == links!!
	public void rotateRobot(int angle) {
		btCommandWrapper.SendCommandAndReceive(Command.ROTATE, angle);

		
	}

	public void rotateSensorMotor(int angle) {
		btCommandWrapper.SendCommandAndReceive(Command.ROTATESENSOR, angle);

	}
	public void resetSensorMotor(){
		btCommandWrapper.SendCommandAndReceive(Command.RESETSENSORMOTOR, 0);

	}

	public int getDistance() {
		return btCommandWrapper.SendCommandAndReceive(Command.GETDISTANCE, 0);		
	}

	public int getDirectionIR() {
		return btCommandWrapper.SendCommandAndReceive(Command.GETIR, 0);

	}

	public int getLightValue() {
		return btCommandWrapper.SendCommandAndReceive(Command.GETLIGHT, 0);

	}
	public ArrayList<Integer> printValues(){
		String output = "";
		int reading = btCommandWrapper.receiveValue();
		ArrayList<Integer> result = new ArrayList<Integer>();
		// -1 = end of barcode
		while (reading != -1) {
			result.add(reading);
			output += reading;
			output +=" ";
			reading = btCommandWrapper.receiveValue();
		}

		System.out.println(output);
		return result;
	}

	public String getBarcode() {
		return btCommandWrapper.receiveBarcodeData();
		
	}


	public boolean hasBlackListValues() {
		
		if(btCommandWrapper.blackList.size()>5)
		{
			btCommandWrapper.blackList.clear();
			return true;
		}
		else return false;
			
	}
	public boolean isPressed(){
		if(btCommandWrapper.isPressed()){
			btCommandWrapper.setPressed(false);
			return true;
		}
		else
			return false;
			
			
	}

	public void setSpeed(int value) {
		btCommandWrapper.SendCommandAndReceive(Command.SETSPEED, value);
	}

	
}
